package com.example.leaderapplication.View.Activity;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.example.leaderapplication.Base.BaseActivity;
import com.example.leaderapplication.Base.BaseFragment;
import com.example.leaderapplication.R;
import com.example.leaderapplication.Utils.LogUtil;
import com.example.leaderapplication.View.Fragment.AnalyseFragment;
import com.example.leaderapplication.View.Fragment.HomePageFragment;
import com.example.leaderapplication.View.Fragment.MyFragment;
import com.example.leaderapplication.Widgets.BottonTabView;
import com.example.leaderapplication.Widgets.NoScrollViewPager;


import java.util.ArrayList;
import java.util.List;

public class MainActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    private List<BaseFragment> tabFragments;
    private List<BottonTabView> tabIndicators;
    private NoScrollViewPager viewPager;
    private BottonTabView homePage;
    private BottonTabView analyse;
    private BottonTabView my;
    private FragmentPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public int setLayoutId() {
        return R.layout.activity_main;
    }


    @Override
    protected void initView() {

        viewPager = (NoScrollViewPager) findViewById(R.id.nsvp_main_view);
        homePage = (BottonTabView) findViewById(R.id.sv_home_page);
        analyse = (BottonTabView) findViewById(R.id.sv_analyse);
        my = (BottonTabView) findViewById(R.id.sv_my);

        if (tabFragments == null) {
            tabFragments = new ArrayList<>();
            tabIndicators = new ArrayList<>();
            tabFragments.add(HomePageFragment.getInstance(baseUrl));
            tabFragments.add(AnalyseFragment.getInstance());
            tabFragments.add(MyFragment.getInstance());
        }

        adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return tabFragments.size();
            }

            @Override
            public Fragment getItem(int arg0) {
                return tabFragments.get(arg0);
            }
        };

        initTabIndicator();

        viewPager.setAdapter(adapter);
        viewPager.setOffscreenPageLimit(2);
        viewPager.addOnPageChangeListener(this);


    }

    @Override
    protected void onResume() {
        super.onResume();
        //
        viewPager.setCurrentItem(0, false);
    }

    @Override
    protected void doSomeThing() {

    }

    private void initTabIndicator() {
        tabIndicators.add(homePage);
        tabIndicators.add(analyse);
        tabIndicators.add(my);

        homePage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(0, false);
            }
        });

        analyse.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(1, false);
            }
        });

        my.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                viewPager.setCurrentItem(2, false);
            }
        });
    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        if (position == 0) {
            tabIndicators.get(position).setTabImage(R.drawable.shouye2);
            tabIndicators.get(position).setTextColor(R.color.color_blue);
        } else {
            tabIndicators.get(0).setTabImage(R.drawable.shouye1);
            tabIndicators.get(0).setTextColor(R.color.color_half_blcak);

        }

        if (position == 1) {
            tabIndicators.get(position).setTabImage(R.drawable.fenxi2);
            tabIndicators.get(position).setTextColor(R.color.color_blue);
        } else {
            tabIndicators.get(1).setTabImage(R.drawable.fenxi1);
            tabIndicators.get(1).setTextColor(R.color.color_half_blcak);
        }

        if (position == 2) {
            tabIndicators.get(position).setTabImage(R.drawable.wode2);
            tabIndicators.get(position).setTextColor(R.color.color_blue);
        } else {
            tabIndicators.get(2).setTabImage(R.drawable.wode1);
            tabIndicators.get(2).setTextColor(R.color.color_half_blcak);
        }

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        startActivity(new Intent(this, LoginActivity.class));
        finish();
        System.exit(0);
    }
}
